﻿using DiaoPaiDaYin;
using DXApplicationTangche.UC.门店下单.DTO;
using mendian;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.service
{
    /// <summary>
    /// 客户服务
    /// </summary>
    public class CustomerService
    {
        /// <summary>
        /// 获取客户基本信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<CustomerInformation> GetCustomerInformation(int id)
        {
            List<CustomerInformation> ci = new List<CustomerInformation>();
            String sql = "SELECT " +
                "fcr.id, " +
                "fcr.CUSTOMER_ID, " +
                "fcr.CUSTOMER_NAME, " +
                "fcr.SEX, " +
                "fcr.AGE, " +
                "fr.STYLE_CATEGORY_CD, " +
                "fr.ITEM_CD, " +
                "fr.ITEM_VALUE, " +
                "fr.FIT_VALUE, " +
                "fpp.PROPERTY_NAME_CN, " +
                "dop.ITEM_NAME_CN  " +
                "FROM " +
                "	a_customer_fit_count_r AS fcr " +
                "	LEFT JOIN a_customer_fit_r fr ON fcr.id = fr.FIT_COUNT_ID " +
                "	LEFT JOIN a_fit_property_p AS fpp ON fr.ITEM_VALUE = fpp.PROPERTY_VALUE " +
                "	LEFT JOIN w_dict_option_p AS dop ON fr.ITEM_VALUE = dop.ITEM_CD  " +
                "	AND fr.FIT_VALUE = dop.ITEM_VALUE  " +
                "WHERE " +
                "	fcr.CUSTOMER_ID = '" + id.ToString() + "' " +
                "	AND fcr.DEFAULT_FLAG = '1'";
            DataTable dt = SQLmtm.GetDataTable(sql);
            if (dt.Rows.Count != 0)
            {
                ci.Add(new CustomerInformation("收件人姓名", dt.Rows[0]["CUSTOMER_NAME"].ToString()));
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["ITEM_NAME_CN"].ToString() != "")
                    {
                        ci.Add(new CustomerInformation(dr["PROPERTY_NAME_CN"].ToString(), dr["ITEM_NAME_CN"].ToString()));
                    }
                    else
                    {
                        ci.Add(new CustomerInformation(dr["PROPERTY_NAME_CN"].ToString(), dr["FIT_VALUE"].ToString()));
                    }
                }
            }
            return ci;
        }

        /// <summary>
        /// 查询用户
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static DataTable GetCustomerData(String str)
        {
            String sql = "SELECT " +
                "	acp.CUSTOMER_ID AS ID, " +
                "	CONCAT( CONCAT( acp.CUSTOMER_FIRST_NAME, ' ' ), acp.CUSTOMER_LAST_NAME ) AS 微信名称, " +
                "	acp.MOBILE AS 手机, " +
                "	acp.CREATE_DATE AS 注册时间, " +
                "	cap.CONSIGNEE AS 客户姓名 " +
                "FROM " +
                "	a_customer_p AS acp " +
                "LEFT JOIN a_customer_address_p AS cap ON cap.CUSTOMER_ID = acp.CUSTOMER_ID " +
                "WHERE " +
                "	acp.CUSTOMER_FIRST_NAME LIKE '%" + str + "%'  " +
                "	OR acp.CUSTOMER_LAST_NAME LIKE '%" + str + "%'  " +
                "	OR acp.MOBILE LIKE '%" + str + "%'  " +
                "	OR cap.CONSIGNEE LIKE '%" + str + "%'  " +
                "ORDER BY " +
                "	acp.CREATE_DATE DESC  ";
            return SQLmtm.GetDataTable(sql);
        }

        /// <summary>
        /// 新增用户
        /// </summary>
        /// <param name="ci"></param>
        /// <param name="information"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static List<CustomerInformation> createCustomer(List<CustomerInformation> ci, String information, String value)
        {
            List<CustomerInformation> customerInformation = new List<CustomerInformation>();
            customerInformation.Add(new CustomerInformation(information, value));
            customerInformation.AddRange(ci);
            return customerInformation;
        }
        /// <summary>
        /// 坚山美国订单导入创建客户用
        /// </summary>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        /// <param name="orderDto"></param>
        public static void Create坚山美国Customer(String firstName, String lastName)
        {
            String FirstName = firstName;
            String LastName = lastName;
            String customer_name = FirstName + LastName;
            DataRow dr_customerid = SQLmtm.GetDataRow("SELECT MAX(CUSTOMER_ID) AS CUSTOMER_ID FROM a_customer_p ");
            int cUSTOMER_ID = Convert.ToInt32(dr_customerid["CUSTOMER_ID"]);
            cUSTOMER_ID++;
            int i1 = SQLmtm.DoInsert("a_customer_p", new string[] { "CUSTOMER_ID", "CUSTOMER_FIRST_NAME", "CUSTOMER_LAST_NAME", "MOBILE", "DEFAULT_ADDR_FLAG", "SEX_CD", "E_MAIL_URL", "DELETE_FLAG", "VERSION", "CUSTOMER_STATUS" },
            new string[] { cUSTOMER_ID.ToString(), FirstName, LastName, "", "0", "SEX-M_10", "", "0", "1", "CUSTOMER_SOURCE-CS_SHOP" });
            if (i1 != 1)
            {
                MessageBox.Show("保存失败");
            }
            //a_customer_fit_count_r           
            int i14 = SQLmtm.DoInsert("a_customer_fit_count_r", new string[] { "CUSTOMER_ID", "CUSTOMER_NAME", "SEX", "AGE", "CREATE_DATE", "DELETE_FLAG", "DEFAULT_FLAG", "DATA_COMPLETE" },
                new string[] { cUSTOMER_ID.ToString(), customer_name, "0", "", DateTime.Now.ToUniversalTime().ToString(), "0", "1", "1" });
            DataRow dr_customer_countid = SQLmtm.GetDataRow("SELECT MAX(ID) AS ID FROM a_customer_fit_count_r");
            int customer_countid = Convert.ToInt32(dr_customer_countid["ID"]);
            if (i14 != 1)
            {
                MessageBox.Show("保存失败");
            }
            //门店客户表
            int i2 = SQLmtm.DoInsert("a_shop_customer_r", new string[] { "SHOP_ID", "CUSTOMER_ID" }, new string[] { "49", cUSTOMER_ID.ToString() });
            if (i2 != 1)
            {
                MessageBox.Show("保存失败");
            }
            //身高
            int i3 = SQLmtm.DoInsert("a_customer_fit_r", new string[] { "CUSTOMER_ID", "STYLE_CATEGORY_CD", "ITEM_CD", "ITEM_VALUE", "DELETE_FLAG", "FIT_VALUE", "FIT_COUNT_ID" },
                new string[] { cUSTOMER_ID.ToString(), "STYLE_CATEGORY-SHIRT", "SHIRT_FITMT004", "FITMT_ITEM_09", "0", "0", customer_countid.ToString() });
            if (i3 != 1)
            {
                MessageBox.Show("保存失败");
            }

            //体重
            int i4 = SQLmtm.DoInsert("a_customer_fit_r", new string[] { "CUSTOMER_ID", "STYLE_CATEGORY_CD", "ITEM_CD", "ITEM_VALUE", "DELETE_FLAG", "FIT_VALUE", "FIT_COUNT_ID" },
                    new string[] { cUSTOMER_ID.ToString(), "STYLE_CATEGORY-SHIRT", "SHIRT_FITMT004", "FITMT_ITEM_10", "0", "0", customer_countid.ToString() });
            //cUSTOMER_FIT_ID++;
            if (i4 != 1)
            {
                MessageBox.Show("保存失败");
            }

            //肩宽CODE
            int i5 = SQLmtm.DoInsert("a_customer_fit_r", new string[] { "CUSTOMER_ID", "STYLE_CATEGORY_CD", "ITEM_CD", "ITEM_VALUE", "DELETE_FLAG", "FIT_VALUE", "FIT_COUNT_ID" },
        new string[] { cUSTOMER_ID.ToString(), "STYLE_CATEGORY-SHIRT", "SHIRT_FITMT004", "FITMT_ITEM_05", "0", "0", customer_countid.ToString() });
            //cUSTOMER_FIT_ID++;
            if (i5 != 1)
            {
                MessageBox.Show("保存失败");
            }

            //胸围CODE
            int i6 = SQLmtm.DoInsert("a_customer_fit_r", new string[] { "CUSTOMER_ID", "STYLE_CATEGORY_CD", "ITEM_CD", "ITEM_VALUE", "DELETE_FLAG", "FIT_VALUE", "FIT_COUNT_ID" },
        new string[] { cUSTOMER_ID.ToString(), "STYLE_CATEGORY-SHIRT", "SHIRT_FITMT004", "FITMT_ITEM_02", "0", "0", customer_countid.ToString() });
            //cUSTOMER_FIT_ID++;
            if (i6 != 1)
            {
                MessageBox.Show("保存失败");
            }

            //腰围CODE
            int i7 = SQLmtm.DoInsert("a_customer_fit_r", new string[] { "CUSTOMER_ID", "STYLE_CATEGORY_CD", "ITEM_CD", "ITEM_VALUE", "DELETE_FLAG", "FIT_VALUE", "FIT_COUNT_ID" },
        new string[] { cUSTOMER_ID.ToString(), "STYLE_CATEGORY-SHIRT", "SHIRT_FITMT004", "FITMT_ITEM_12", "0", "0", customer_countid.ToString() });
            //cUSTOMER_FIT_ID++;
            if (i7 != 1)
            {
                MessageBox.Show("保存失败");
            }
            //臀围CODE
            int i8 = SQLmtm.DoInsert("a_customer_fit_r", new string[] { "CUSTOMER_ID", "STYLE_CATEGORY_CD", "ITEM_CD", "ITEM_VALUE", "DELETE_FLAG", "FIT_VALUE", "FIT_COUNT_ID" },
        new string[] { cUSTOMER_ID.ToString(), "STYLE_CATEGORY-SHIRT", "SHIRT_FITMT004", "FITMT_ITEM_03", "0", "0", customer_countid.ToString() });
            //cUSTOMER_FIT_ID++;
            if (i8 != 1)
            {
                MessageBox.Show("保存失败");
            }

            //身长CODE
            int i9 = SQLmtm.DoInsert("a_customer_fit_r", new string[] { "CUSTOMER_ID", "STYLE_CATEGORY_CD", "ITEM_CD", "ITEM_VALUE", "DELETE_FLAG", "FIT_VALUE", "FIT_COUNT_ID" },
        new string[] { cUSTOMER_ID.ToString(), "STYLE_CATEGORY-SHIRT", "SHIRT_FITMT004", "FITMT_ITEM_04", "0", "0", customer_countid.ToString() });
            //cUSTOMER_FIT_ID++;
            if (i9 != 1)
            {
                MessageBox.Show("保存失败");
            }

            //领围CODE
            int i10 = SQLmtm.DoInsert("a_customer_fit_r", new string[] { "CUSTOMER_ID", "STYLE_CATEGORY_CD", "ITEM_CD", "ITEM_VALUE", "DELETE_FLAG", "FIT_VALUE", "FIT_COUNT_ID" },
        new string[] { cUSTOMER_ID.ToString(), "STYLE_CATEGORY-SHIRT", "SHIRT_FITMT004", "FITMT_ITEM_01", "0", "0", customer_countid.ToString() });
            //cUSTOMER_FIT_ID++;
            if (i10 != 1)
            {
                MessageBox.Show("保存失败");
            }

            //腕围CODE
            int i11 = SQLmtm.DoInsert("a_customer_fit_r", new string[] { "CUSTOMER_ID", "STYLE_CATEGORY_CD", "ITEM_CD", "ITEM_VALUE", "DELETE_FLAG", "FIT_VALUE", "FIT_COUNT_ID" },
        new string[] { cUSTOMER_ID.ToString(), "STYLE_CATEGORY-SHIRT", "SHIRT_FITMT004", "FITMT_ITEM_08", "0", "0", customer_countid.ToString() });
            //cUSTOMER_FIT_ID++;
            if (i11 != 1)
            {
                MessageBox.Show("保存失败");
            }

            //袖长CODE
            int i12 = SQLmtm.DoInsert("a_customer_fit_r", new string[] { "CUSTOMER_ID", "STYLE_CATEGORY_CD", "ITEM_CD", "ITEM_VALUE", "DELETE_FLAG", "FIT_VALUE", "FIT_COUNT_ID" },
        new string[] { cUSTOMER_ID.ToString(), "STYLE_CATEGORY-SHIRT", "SHIRT_FITMT004", "FITMT_ITEM_06", "0", "0", customer_countid.ToString() });
            //cUSTOMER_FIT_ID++;
            if (i12 != 1)
            {
                MessageBox.Show("保存失败");
            }

            //上臂袖肥CODE
            int i13 = SQLmtm.DoInsert("a_customer_fit_r", new string[] { "CUSTOMER_ID", "STYLE_CATEGORY_CD", "ITEM_CD", "ITEM_VALUE", "DELETE_FLAG", "FIT_VALUE", "FIT_COUNT_ID" },
        new string[] { cUSTOMER_ID.ToString(), "STYLE_CATEGORY-SHIRT", "SHIRT_FITMT004", "FITMT_ITEM_07", "0", "0", customer_countid.ToString() });
            //cUSTOMER_FIT_ID++;
            if (i13 != 1)
            {
                MessageBox.Show("保存失败");
            }

            DataRow dr_addressid = SQLmtm.GetDataRow("SELECT MAX(ADDRESS_ID) AS ADDRESS_ID FROM a_customer_address_p");
            int aDDRESS_ID = Convert.ToInt32(dr_addressid["ADDRESS_ID"]);
            aDDRESS_ID++;
            SQLmtm.DoInsert("a_customer_address_p", new string[] { "ADDRESS_ID", "CUSTOMER_ID", "DEFAULT_ADDR_FLAG", "CONSIGNEE", "ADDRESS_PROVINCE_CD", "ADDRESS_CITY_CD", "ADDRESS_DISTRICT", "ADDRESS_DETAIL", "ZIP_CODE", "MOBILE", "DELETE_FLAG" },
                new string[] { aDDRESS_ID.ToString(), cUSTOMER_ID.ToString(), "1", customer_name, "", "", "", "", "", "", "0" });
            String listFitData = null;
            try
            {
                DataTable dt = SQLmtm.GetDataTable("SELECT CUSTOMER_ID,ITEM_CD,ITEM_VALUE,FIT_VALUE FROM `a_customer_fit_r` WHERE FIT_COUNT_ID='" + customer_countid.ToString() + "'");
                foreach (DataRow dr in dt.Rows)
                {
                    listFitData = listFitData + dr["ITEM_CD"].ToString() + "-" + dr["ITEM_VALUE"].ToString() + ":" + dr["FIT_VALUE"] + ",";
                }
            }
            catch
            {
                MessageBox.Show("保存失败");
            }
            FitValueCalculate fvc = new FitValueCalculate(customer_countid.ToString(), listFitData);

            String fjsondata = Newtonsoft.Json.JsonConvert.SerializeObject(fvc);
            //String entryDataJson = Newtonsoft.Json.JsonConvert.SerializeObject(class1);
            //RestCall.PostHttp("https://shirtmtm.com/fragsmart-mtm/customer/update/update", fjsondata);
            //RestCall.PostHttp("http://localhost:8080/customer/update/update", fjsondata);

            Frm客户.cUSTOMER_ID = cUSTOMER_ID;
            Frm客户.customer_name = customer_name;
            Frm客户.aDDRESS_ID = aDDRESS_ID;
            Frm客户.customer_countid = customer_countid;
        }
    }
}
